//
//  LBExclusionTextureFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 3/29/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBExclusionTextureFilter.h"
#include "LBGraphics.h"

char fShaderStrExclusionTexture[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform sampler2D texture2;"
"uniform float opacity;"


// exclusion function
"vec4 exclusion(vec4 base, vec4 blend, float opacity){"
"   vec4 result =  base;"

"   result.r = 0.5 - 2.0* (base.r - 0.5)*(blend.r - 0.5);"
"   result.g = 0.5 - 2.0* (base.g - 0.5)*(blend.g - 0.5);"
"   result.b = 0.5 - 2.0* (base.b - 0.5)*(blend.b - 0.5);"

"   result = mix(base, result, opacity);"

"   return result;"

"}"
// END exclusion function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    lowp vec4 blendColor = texture2D(texture2, vtexCoor);"
"    textureColor = exclusion(textureColor, blendColor, opacity);"
"    gl_FragColor = textureColor;"
"}";

LBExclusionTextureFilter::LBExclusionTextureFilter(){
    init(0, fShaderStrExclusionTexture);
    LBGraphics::getInstance()->useProgram(program);
    opacityUniform = program->uniformIndex("opacity");
    setOpactiy(1.0f);
}